package com.situ.onlinebookstore.front.config;


import com.situ.onlinebookstore.common.util.SnowflakeUtil;
import com.situ.onlinebookstore.front.interceptor.UserInterceptor;
import javafx.concurrent.Worker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Value("${upload.path}")
    private String path;

    @Value("${snow.work-id}")
    private Long workId;
    @Value("${snow.data-center-id}")
    private Long datacenterId;
    @Resource
    private UserInterceptor userInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

        //图片路径映射
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+path);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInterceptor)
                .addPathPatterns(
                        "/user/**",
                        "/uc/**",
                        "/api/user/**",
                        "/api/uc/**"
                )
                .excludePathPatterns(
                        "/user/login",//放行页面
                        "/user/sign-in",
                        "/api/user/login",//放行接口
                        "/api/user/reg"
                );
    }

    //生成一个对象，放到Spring容器中
    @Bean //将当前方法的返回对象放到Spring容器中
    public SnowflakeUtil snowflakeUtil(){
        return new SnowflakeUtil(workId,datacenterId);
    }
}
